﻿using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using Common.ParameterLocator;

namespace SpecFlowTfsLinker.Cleaner
{
    public class TextToMethodCleaner : Cleaner
    {
        public TextToMethodCleaner(string statementToClean)
            : base(statementToClean)
        {
        }

        public override string Clean()
        {
            StatementToClean = DeleteExternalWhiteSpace();
            StatementToClean = StatementToClean.ReplaceWordChars();
            RemoveParameters();
            TransformAccent();
            DeleteSpecialCharacter();
            StatementToClean = StatementToClean.ToTitleCase();
            StatementToClean = StatementToClean.Replace(" ", "");
            return StatementToClean;
        }

        private void DeleteSpecialCharacter()
        {
          StatementToClean =  Regex.Replace(StatementToClean, "[^A-Za-z0-9\\s]", "");
        }

        private void TransformAccent()
        {
            StatementToClean = new string(StatementToClean.Normalize(NormalizationForm.FormD)
                                             .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
                                             .ToArray()).Normalize(NormalizationForm.FormC);
        }

        private void RemoveParameters()
        {
            StatementToClean = Regex.Replace(StatementToClean, ServiceParameter.Instance.Parameter.RegexFindParameter, "");
        }
    }
}